package io.github.wanggit.antrpc.gateway.web.utils;

import io.github.wanggit.antrpc.commons.utils.JsonUtils;
import io.github.wanggit.antrpc.gateway.web.vo.Result;
import org.apache.commons.codec.digest.DigestUtils;

/** 校验码 */
public abstract class CheckSumUtil {

    private static final String RANDOM_SEED =
            "5c70b2734f6767ffeb5c7a83e1b30e89541247e0aacee8ea840b940c15b39276";

    /**
     * @param result result vm
     * @return checksum
     */
    public static String checksum(Result result) {
        String content =
                result.getMsg() + result.getCode() + JsonUtils.toJsonString(result.getData());
        return internalChecksum(content);
    }

    /**
     * @param content content
     * @return md5
     */
    private static String internalChecksum(String content) {
        return DigestUtils.md5Hex(RANDOM_SEED + content.length());
    }
}
